package com.syq;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Sun Yueqi
 * @version 1.00
 * @time 2022/6/8 20:08
 */
public class TestStream2 {
    public static void main(String[] args) {
        //limit限流
//        new Random().ints().limit(10).forEach(System.err::println);

        //distinct去重
//        Stream.of("1","123","123","1","45646","4845")
//                .distinct()
//                .forEach(System.err::println);
        //findFirst获取第一个数据
//        System.err.println(Stream.of("123456", "123", "123", "1", "45646", "4845")
//                .findFirst()
//                .get());
        //findAny寻找任意一个
//        System.err.println(Stream.of("123456", "123", "123", "1", "45646", "4845")
//                .findAny()
//                .get());

        List<Grade> grades=new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Grade grade=new Grade();
            grade.setName("00"+i);

            List<Student> students=new ArrayList<>();
            for (int j = 0; j < 20; j++) {
                Student student=new Student();
                student.setName(grade.getName()+"->"+j);
                student.setAge(j+10);
                students.add(student);
            }
            grade.setStudents(students);
            grades.add(grade);
        }

        List<Student> studentList = grades.stream()
                .flatMap(grade -> grade.getStudents()
                        .stream())
                .collect(Collectors.toList());
//        System.err.println(studentList);

        List<Character> characterList = Stream.of("my name is qiaqia")
                .map(s -> s.split(" "))
                //从数组转成集合
                .flatMap(Arrays::stream)
                .flatMap(s -> s.chars().mapToObj(c->(char)c))
                        .collect(Collectors.toList());
//        System.err.println(characterList);

        //拼接
        String s=Stream.of("1", "22", "333", "abc", "1", "2", "333", "abc", "4").reduce((s1, s2) -> s1+s2).get();
//        System.err.println(s);
        boolean allMatch = Stream.of("1", "22", "333", "abc", "1", "2", "333", "abc", "4").allMatch(s1 -> s1.length() > 2);
        boolean anyMatch = Stream.of("1", "22", "333", "abc", "1", "2", "333", "abc", "4").anyMatch(s1 -> s1.length() > 2);
        boolean noneMatch = Stream.of("1", "22", "333", "abc", "1", "2", "333", "abc", "4").noneMatch(s1 -> s1.length() > 3);
//        System.err.println(allMatch +"\t"+ anyMatch + "\t"+noneMatch);
        //去重后排序
        List<String> sortList = Stream.of("1", "22", "333", "abc", "1", "2", "333", "abc", "4").distinct().sorted().collect(Collectors.toList());
//        System.err.println(sortList);

        List<Student> studentList1 = studentList.stream().sorted(Comparator.comparingInt(Student::getAge)).collect(Collectors.toList());
//        System.err.println(studentList1);
    }
}
